<?php
namespace Home\Controller;
use Think\Controller;
class AddressController extends Controller {

	//查看地址
    public function lookaddress(){
       $address=M("Address");
       $userid = $_POST["addressuserid"];
       $newdata["addressuser_id"]=$userid;
       $data=$address->where($newdata)->select();
       
       if ($data) {
              $this->ajaxReturn(array("code"=>200,"data"=>$data),"JSON");
       }else{
              $this->ajaxReturn(array("code"=>500,"data"=>NULL),"JSON");
       }    
    }
    //http://localhost:18181/NongGuoGuo/index.php/home/address/addaddress
    //修改地址
    public function updateaddress(){
       $address=M("Address");

       $addressid=$_POST["addressid"];
       $addressname=$_POST["address"];
       $username=$_POST["username"];
       $userphone=$_POST["phone"];

       $newdata["address_id"]=$addressid;
       $newdata["address_name"]=$addressname;
       $newdata["address_userPhone"]=$userphone;
       $newdata["address_userName"]=$username;

       $result=$address->save($newdata);//当数据相同时，无法修改，即为原数据    
       if ($result) {
          $this->ajaxReturn(array("code"=>200,"message"=>"修改成功","data"=>$result),"JSON");
       }else{
            $this->ajaxReturn(array("code"=>500,"message"=>"修改失败","data"=>NULL),"JSON");
       }  
    }
    //新增地址
    public function addaddress(){

       $address=M("Address");

      $addname=$_POST["address"];
      $name=$_POST["username"];
      $phone=$_POST["userphone"];
      $userid=$_POST["userid"];

       $newdata["address_name"]=$addname;
       $newdata["address_userPhone"]=$phone;
       $newdata["address_userName"]=$name;
       $newdata["addressuser_id"]=$userid;

       $result=$address->add($newdata);       
       if ($result) {
          $this->ajaxReturn(array("code"=>200,"message"=>"新增成功","data"=>$result),"JSON");
       }else{
            $this->ajaxReturn(array("code"=>500,"message"=>"新增失败","data"=>NULL),"JSON");
       }  

    }
    //删除收货地址
    public function removeaddress(){
       $address=M("Address");
       $id=$_POST["addressid"];
       $newdata["address_id"]=$id;
       $result=$address->where($newdata)->delete();//当数据相同时，无法修改，即为原数据       
       if ($result) {
          $this->ajaxReturn(array("code"=>200,"message"=>"删除成功","data"=>$result),"JSON");
       }else{
            $this->ajaxReturn(array("code"=>500,"message"=>"删除失败","data"=>NULL),"JSON");
       }  
    }
    //选择默认地址
    public function chooseaddrss(){

       $user=M("User");
       $id=$_POST["userid"];
       $addressid=$_POST["addressid"];  
       $newdata["user_id"]=$id;
       $newdata["address_id"]=$addressid;
       $result=$user->save($newdata);//当数据相同时，无法修改，即为原数据

      // var_dump($data);       
       if ($result) {
          $this->ajaxReturn(array("code"=>200,"message"=>"修改成功","data"=>$result[0]),"JSON");
       }else{
            $this->ajaxReturn(array("code"=>500,"message"=>"修改失败","data"=>$data[0]),"JSON");
       }


    	
    }
    //查询所有的地址id和名称
    function inqureAlladdress(){
      $address=M("Address");
      $result=$address
       ->field("address_id,address_name")
       ->select();
       if ($result) {
          $this->ajaxReturn(array("code"=>200,"message"=>"查询成功","data"=>$result),"JSON");
       }else{
            $this->ajaxReturn(array("code"=>500,"message"=>"查询失败","data"=>NULL),"JSON");
       } 

    }

   














}